home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / comps / widgets / delphi10 / filicpnl / filicpnl.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-06-18  |  16.0 KB  |  512 lines

  1. unit Filicpnl;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls, ShellAPI, ResUnit;
  8.  
  9. type
  10.   TFileIconPanel = class(TPanel)
  11.   private
  12.     { Private declarations }
  13.   protected                                   { event method procedure.      }
  14.     { Protected declarations }
  15.   public
  16.     FHighlightColor : TColor;                 { This holds bright edge bevel }
  17.     FShadowColor    : TColor;                 { This holds dark edge bevel   }
  18.     FTheIcon : TIcon;                         { This is the display icon    }
  19.     FTheName : String;                        { This is the filename        }
  20.     FTheLabel : TLabel;                       { This is the display label   }
  21.     Oldname : string;
  22.     OldWidth ,
  23.     Oldheight : Integer;
  24.     { Public declarations }
  25.     Selected : Boolean;                       { This holds selection status }
  26.     procedure Paint; override;                { This allows custom painting  }
  27.     procedure GetColorsForFileIcon( TheFile : String;
  28.                var BC , HC , SC , TC : TColor );
  29.     constructor Create(AOwner : TComponent); override; { override create    }
  30.     procedure Initialize( PanelX              ,             { Left          }
  31.                           PanelY              ,             { Top           }
  32.                           PanelWidth          ,             { Width         }
  33.                           PanelHeight         ,             { Height        }
  34.                           PanelBevelWidth     ,             { Bevel Width   }
  35.                           LabelFontSize         : Integer;  { Font size     }
  36.                           PanelColor          ,             { Main color    }
  37.                           PanelHighlightColor ,             { Bright color  }
  38.                           PanelShadowColor    ,             { Dark color    }
  39.                           LabelTextColor        : TColor;   { Text color    }
  40.                           TheFilename         ,             { Filename      }
  41.                           LabelFontName         : String;   { Font name     }
  42.                           LabelFontStyle        : TFontStyles;  { Font style}
  43.                           ExtraData             : Integer       ); virtual;
  44.     destructor Destroy; override;             { override destroy to free    }
  45.     procedure InitTheFIP;
  46.   published
  47.       property TheName : String read FTheName write FTheName;
  48.   end;
  49.  
  50. procedure Register;
  51.  
  52. implementation
  53.  
  54.  
  55. procedure TFileIconPanel.GetColorsForFileIcon( TheFile : String;
  56.            var BC , HC , SC , TC : TColor );
  57. var AmADir      ,             { Booleans hold file attribs }
  58.     AmAnArchive ,
  59.     AmAVolumeId ,
  60.     AmHidden    ,
  61.     AmReadOnly  ,
  62.     AmSystem      : Boolean;
  63. { This procedure sets the imported booleans to the file's attributes }
  64. procedure GetFileAttributes( TheFile : String; var IsDirectory ,
  65.            IsArchive , IsVolumeID , IsHidden , IsReadOnly ,
  66.             IsSysFile : Boolean );
  67. var TheResult : Integer; { Traps for error code on VolumeID }
  68. begin
  69.   { Clear the imported flags for default }
  70.   IsDirectory := false;
  71.   IsArchive := false;
  72.   IsVolumeID := false;
  73.   IsHidden := False;
  74.   IsReadOnly := false;
  75.   IsSysFile := false;
  76.   { Make the Dos call }
  77.   TheResult := FileGetAttr( TheFile );
  78.   if TheResult < 0 then
  79.   begin
  80.     { Volume ID returns -2 (?) }
  81.     IsVolumeID := true;
  82.     { It has no other properties }
  83.     exit;
  84.   end;
  85.   { Use AND test to set all other properties }
  86.   if (( TheResult and faDirectory ) = faDirectory ) then IsDirectory := true;
  87.   if (( TheResult and faArchive ) = faArchive ) then IsArchive := true;
  88.   if (( TheResult and faVolumeID ) = faVolumeID ) then IsVolumeID := true;
  89.   if (( TheResult and faReadOnly ) = faReadOnly ) then IsReadOnly := true;
  90.   if (( TheResult and faHidden ) = faHidden ) then IsHidden := true;
  91.   if (( TheResult and faSysFile ) = faSysFile ) then IsSysFile := true;
  92. end;
  93. begin
  94.   if TheFile = 'NO FILE' then
  95.   begin
  96.     BC := clSilver;
  97.     HC := clWhite;
  98.     SC := clGray;
  99.     TC := clBlack;
  100.     exit;
  101.   end;
  102.   { Make the call to internal fileworkbench to set attributes }
  103.   GetFileAttributes( TheFile , AmADir , AmAnArchive , AmAVolumeId ,
  104.    AmHidden , AmReadOnly , AmSystem );
  105.   { Volume ID has no subtypes }
  106.   if AmAVolumeID then
  107.   begin
  108.     BC := clOlive;
  109.     HC := clYellow;
  110.     SC := clBlack;
  111.     TC := clWhite;
  112.     exit;
  113.   end;
  114.   { Check all directory combinations }
  115.   if AmADir then
  116.   begin
  117.     BC := clNavy;
  118.     HC := clBlue;
  119.     SC := clBlack;
  120.     TC := clWhite;
  121.     if AmHidden then
  122.     begin
  123.       if AmReadOnly then
  124.       begin
  125.         if AmSystem then
  126.         begin { One HECK of a file! }
  127.           BC := clBlack;
  128.           HC := clSilver;
  129.           SC := clGray;
  130.           TC := clWhite;
  131.         end
  132.         else
  133.         begin { Dir,RO,Hid }
  134.           BC := clMaroon;
  135.           HC := clFuchsia;
  136.           SC := clGreen;
  137.           TC := clWhite;
  138.         end;
  139.       end
  140.       else
  141.       begin { Dir,Hid }
  142.         BC := clPurple;
  143.         HC := clFuchsia;
  144.         SC := clBlack;
  145.         TC := clWhite;
  146.       end;
  147.     end
  148.     else
  149.     begin
  150.       if AmReadOnly then
  151.       begin
  152.         if AmSystem then
  153.         begin { Dir,RO,Sys }
  154.           BC := clMaroon;
  155.           HC := clLime;
  156.           SC := clGreen;
  157.           TC := clWhite;
  158.         end
  159.         else
  160.         begin { Dir,RO }
  161.           BC := clGreen;
  162.           HC := clLime;
  163.           SC := clBlack;
  164.           TC := clWhite;
  165.         end;
  166.       end
  167.       else
  168.       begin
  169.         if AmSystem then
  170.         begin { Dir,Sys }
  171.           BC := clMaroon;
  172.           HC := clRed;
  173.           SC := clBlack;
  174.           TC := clWhite;
  175.         end;
  176.       end;
  177.     end;
  178.   end
  179.   else { Archive Only; check all combinations }
  180.   begin
  181.     BC := clSilver;
  182.     HC := clWhite;
  183.     SC := clGray;
  184.     TC := clBlack;
  185.     if AmHidden then
  186.     begin
  187.       if AmReadOnly then
  188.       begin
  189.         if AmSystem then
  190.         begin { Hid,RO,Sys }
  191.           BC := clRed;
  192.           HC := clLime;
  193.           SC := clPurple;
  194.           TC := clBlack;
  195.         end
  196.         else
  197.         begin { RO,Hid }
  198.           BC := clLime;
  199.           HC := clFuchsia;
  200.           SC := clMaroon;
  201.           TC := clBlack;
  202.         end;
  203.       end
  204.       else
  205.       begin { Hid }
  206.         BC := clFuchsia;
  207.         HC := clWhite;
  208.         SC := clPurple;
  209.         TC := clBlack;
  210.       end;
  211.     end
  212.     else
  213.     begin
  214.       if AmReadOnly then
  215.       begin
  216.         if AmSystem then
  217.         begin { RO,Sys }
  218.           BC := clRed;
  219.           HC := clLime;
  220.           SC := clMaroon;
  221.           TC := clBlack;
  222.         end
  223.         else
  224.         begin { RO }
  225.           BC := clLime;
  226.           HC := clWhite;
  227.           SC := clGreen;
  228.           TC := clBlack;
  229.         end;
  230.       end
  231.       else
  232.       begin
  233.         if AmSystem then
  234.         begin { System }
  235.           BC := clRed;
  236.           HC := clWhite;
  237.           SC := clMaroon;
  238.           TC := clBlack;
  239.         end;
  240.       end;
  241.     end;
  242.   end;
  243. end;
  244.  
  245.  
  246. procedure TFileIconPanel.InitTheFIP;
  247. var ButtonColor   ,                    { main panel color       }
  248.     ButtonHLColor ,                    { bright panel color     }
  249.     ButtonSColor  ,                    { dark panel color       }
  250.     Textcolor       : TColor;          { label text color       }
  251.     TheWidth , TheHeight : Integer;
  252. begin
  253.   TheWidth := Width;
  254.   if TheWidth < 90 then TheWidth := 90;
  255.   TheHeight := Height;
  256.   if TheHeight < 90 then TheHeight := 90;
  257.   OldWidth := TheWidth;
  258.   Oldheight := TheHeight;
  259.   Width := TheWidth;
  260.   Height := TheHeight;
  261.   GetColorsForFileIcon( TheName , ButtonColor ,
  262.    ButtonHLColor , ButtonSColor , TextColor );
  263.   Initialize( Left , Top , TheWidth , TheHeight , 3 ,
  264.     7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor , TheName
  265.      , 'MS Serif' , [] , 0 );
  266.   oldName := TheName;
  267. end;
  268.  
  269. { Create method for FIP                                }
  270. constructor TFileIconPanel.Create( AOwner : TComponent );
  271. begin
  272.   { call inherited -- VITAL! }
  273.   inherited Create( AOwner );
  274.   Parent := TWinControl( Aowner );
  275.   { create icon and label components, making self owner/displayer }
  276.   FTheIcon := TIcon.Create;
  277.   FTheLabel := TLabel.Create( Self );
  278.   FThelabel.Parent := Self;
  279.   { Set own and labels mouse methods to stored methods }
  280.   { Set alignment and autosize properties of the label }
  281.   FTheLabel.Autosize := false;
  282.   FTheLabel.Alignment := taCenter;
  283.   { Set selected to false }
  284.   Selected := false;
  285.   Height := 80;
  286.   Width := 80;
  287.   TheName := 'NO FILE';
  288.   InitTheFIP;
  289. end;
  290.  
  291. { Initialization method for FIP                                         }
  292. procedure TFileIconPanel.Initialize( PanelX              ,
  293.                                      PanelY              ,
  294.                                      PanelWidth          ,
  295.                                      PanelHeight         ,
  296.                                      PanelBevelWidth     ,
  297.                                      LabelFontSize         : Integer;
  298.                                      PanelColor          ,
  299.                                      PanelHighlightColor ,
  300.                                      PanelShadowColor    ,
  301.                                      LabelTextColor        : TColor;
  302.                                      TheFilename         ,
  303.                                      LabelFontName         : String;
  304.                                      LabelFontStyle        : TFontStyles;
  305.                                      ExtraData             : Integer );
  306.  
  307. var TheLabelHeight ,             { Holder for label pixel height }
  308.     TheLabelWidth    : Integer;  { Holder for label pixel width  }
  309.     TheOtherPChar    : PChar;    { Windows ASCIIZ string         }
  310. begin
  311.   { Set the basic properties based on imported parameters }
  312.   Left := PanelX;
  313.   Top := PanelY;
  314.   Width := PanelWidth;
  315.   Height := PanelHeight;
  316.   Color := PanelColor;
  317.   BevelWidth := PanelBevelWidth;
  318.   FHighlightColor := PanelHighlightColor;
  319.   FShadowColor := PanelShadowColor;
  320.   FTheName := TheFilename;
  321.   { If the ExtraData field is non-0 then a drive is being sent in }
  322.   if ExtraData <> 0 then
  323.   begin
  324.     GetIconForDrive( ExtraData , FTheIcon );
  325.     { The FileNme property is already set up for the caption; use directly }
  326.     FTheLabel.Caption := TheFilename;
  327.     { Set up the hint for later use (make sure to set ShowHint) }
  328.     Hint := 'Change to ' + TheFileName;
  329.     ShowHint := true;
  330.     { Set up all imported label properties and center it for drawing }
  331.     with FTheLabel do
  332.     begin
  333.       Font.Name := LabelFontName;
  334.       Font.Size := LabelFontSize;
  335.       Font.Style := LabelFontStyle;
  336.       Font.Color := LabelTextColor;
  337.       Canvas.Brush.Color := PanelColor;
  338.       Canvas.Font := Font;
  339.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  340.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  341.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  342.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  343.       Top := Top + Round( Self.Height * 0.75 );
  344.       Height := TheLabelHeight;
  345.       Width := TheLabelWidth;
  346.     end;
  347.   end
  348.   else
  349.   begin
  350.     { A file or directory has been sent in; use GetIconForFile to obtain an }
  351.     { icon either from the file, its owner, or a RES file default.          }
  352.     GetIconForFile( FTheName , FTheIcon );
  353.     if FTheName = 'NO FILE' then
  354.     begin
  355.       FTheLabel.Caption := 'NO FILE';
  356.       Hint := 'Not Initialized';
  357.     end
  358.     else
  359.     begin
  360.       { Check for the Backup caption and set it specially }
  361.       if ExtractfileName( FThename ) = '..' then
  362.       begin
  363.         FTheLabel.Caption := '..';
  364.         Hint := 'Up One Level';
  365.       end
  366.       else
  367.       begin
  368.         { Otherwise just get the filename for the label caption }
  369.         { And the full path for the hint (used later.)          }
  370.         FTheLabel.caption := ExtractFileName( UpperCase( FTheName ));
  371.         Hint := FTheName;
  372.       end;
  373.     end;
  374.     { Activate showhint so hints are seen }
  375.     ShowHint := true;
  376.     { Set label properties with imported values and center for display }
  377.     with FTheLabel do
  378.     begin
  379.       Font.Name := LabelFontName;
  380.       Font.Size := LabelFontSize;
  381.       Font.Style := LabelFontStyle;
  382.       Font.Color := LabelTextColor;
  383.       Canvas.Brush.Color := PanelColor;
  384.       Canvas.Font := Font;
  385.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  386.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  387.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  388.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  389.       Top := Top + Round( Self.Height * 0.75 );
  390.       Height := TheLabelHeight;
  391.       Width := TheLabelWidth;
  392.     end;
  393.   end;
  394. end;
  395.  
  396. { Destroy method for FIP }
  397. destructor TFileIconPanel.Destroy;
  398. begin
  399.   { free component resources }
  400.   FTheIcon.Free;
  401.   FTheLabel.Free;
  402.   { call inherited -- VITAL! }
  403.   inherited Destroy;
  404. end;
  405.  
  406. { Paint method for FIP; overrides normal paint }
  407. procedure TFileIconPanel.Paint;
  408. var
  409.   TheOtherRect   : TRect;   { Holds clientrect   }
  410.   TopColor     ,            { Holds bright color }
  411.   BottomColor    : TColor;  { Holds dark color   }
  412.  
  413. { These methods are from Borland Intl., copyright 1995 }
  414. procedure Frame3D(    Canvas       : TCanvas;
  415.                   var TheRect      : TRect;
  416.                       TopColor   ,
  417.                       BottomColor  : TColor;
  418.                       Width        : Integer );
  419.  
  420. procedure DoRect;
  421. var
  422.   TopRight, BottomLeft: TPoint;
  423. begin
  424.   with Canvas, TheRect do
  425.   begin
  426.     TopRight.X := Right;
  427.     TopRight.Y := Top;
  428.     BottomLeft.X := Left;
  429.     BottomLeft.Y := Bottom;
  430.     Pen.Color := TopColor;
  431.     PolyLine([BottomLeft, TopLeft, TopRight]);
  432.     Pen.Color := BottomColor;
  433.     Dec(BottomLeft.X);
  434.     PolyLine([TopRight, BottomRight, BottomLeft]);
  435.   end;
  436. end;
  437.  
  438. begin
  439.   Canvas.Pen.Width := 1;
  440.   Dec(TheRect.Bottom); Dec(TheRect.Right);
  441.   while Width > 0 do
  442.   begin
  443.     Dec(Width);
  444.     DoRect;
  445.     InflateRect(TheRect, -1, -1);
  446.   end;
  447.   Inc(TheRect.Bottom); Inc(TheRect.Right);
  448. end;
  449.  
  450. procedure AdjustColors(Bevel: TPanelBevel);
  451. begin
  452.   TopColor := FHighlightColor;
  453.   if Bevel = bvLowered then TopColor := FShadowColor;
  454.   BottomColor := FShadowColor;
  455.   if Bevel = bvLowered then BottomColor := FHighlightColor;
  456. end;
  457.  
  458. { Custom code begins here }
  459. begin
  460.   if OldName <> TheName then InitTheFIP;
  461.   if (( OldWidth <> Width ) or ( OldHeight <> Height )) then InitTheFIP;
  462.   { Get the rectangle of the control with API/method call }
  463.   TheOtherRect := GetClientRect;
  464.   { draw basic rectangle with basic color }
  465.   with Canvas do
  466.   begin
  467.     Brush.Color := Color;
  468.     FillRect(TheOtherRect);
  469.   end;
  470.   { Set up for top "icon" frame  and draw it with frame3d }
  471.   TheOtherRect.Right := Width;
  472.   TheOtherRect.Bottom := Round( Height * 0.75 ) - 6 ;
  473.   if BevelOuter <> bvNone then
  474.   begin
  475.     AdjustColors(BevelOuter);
  476.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  477.   end;
  478.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  479.   if BevelInner <> bvNone then
  480.   begin
  481.     AdjustColors(BevelInner);
  482.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  483.   end;
  484.   { Do the same for the lower "label" frame }
  485.   TheOtherRect.Top := Round( Height * 0.75 ) - 5;
  486.   TheOtherRect.Left := 0;
  487.   TheOtherRect.Bottom := Height;
  488.   TheOtherRect.Right := Width;
  489.   if BevelOuter <> bvNone then
  490.   begin
  491.     AdjustColors(BevelOuter);
  492.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  493.   end;
  494.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  495.   if BevelInner <> bvNone then
  496.   begin
  497.     AdjustColors(BevelInner);
  498.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  499.   end;
  500.   { Then draw the icon using canvas draw method }
  501.   Canvas.Draw( (( Width - 32 ) div 2 ) + 1 ,
  502.   ((( Round( Height * 0.75 ) - 6 ) - 32 ) div 2 ) + 1 , FTheIcon );
  503. end;
  504.  
  505.  
  506. procedure Register;
  507. begin
  508.   RegisterComponents('Widgets', [TFileIconPanel]);
  509. end;
  510.  
  511. end.
  512.